import java.io.*;
import java.util.ArrayList;

import javax.servlet.*;
import javax.servlet.http.*;

/**
 * Simple servlet for testing deployment on server.
 * <p>
 * From <a href="http://courses.coreservlets.com/Course-Materials/">the
 * coreservlets.com tutorials on servlets, JSP, Struts, JSF, Ajax, GWT, Spring,
 * Hibernate/JPA, and Java programming</a>.
 */

public class Statistics extends HttpServlet {

  @Override
  public void doGet(HttpServletRequest request, HttpServletResponse response)
      throws ServletException, IOException {

    String id = (String) request.getParameter("id");

    if (id != null && id.equals("opinionstat")) {

      ArrayList<String> pos = QueryProcess.getInstance().getPositiveOpinions();
      ArrayList<String> neg = QueryProcess.getInstance().getNegativeOpinions();
      ArrayList<String> ntr = QueryProcess.getInstance().getNeutralOpinions();

      StringBuffer posComment = new StringBuffer();

      for (int i = 0; i < pos.size(); i++) {
        String[] index = pos.get(i).split(";;");
        for (int j = 0; j < index.length; j++) {
          posComment.append(index[j]);
          posComment.append("<br>");
        }
        posComment.append("<br><br><br>");
      }

      StringBuffer negComment = new StringBuffer();

      for (int i = 0; i < neg.size(); i++) {
        String[] index = neg.get(i).split(";;");
        for (int j = 0; j < index.length; j++) {
          negComment.append(index[j]);
          negComment.append("<br>");
        }
        negComment.append("<br><br><br>");
      }

      StringBuffer ntrComment = new StringBuffer();

      for (int i = 0; i < ntr.size(); i++) {
        String[] index = ntr.get(i).split(";;");
        for (int j = 0; j < index.length; j++) {
          ntrComment.append(index[j]);
          ntrComment.append("<br>");
        }
        ntrComment.append("<br><br><br>");
      }

      response.setContentType("text/html");
      PrintWriter out = response.getWriter();
      String docType = "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.0 "
          + "Transitional//EN\">\n";
      out.println(docType
          + "<HTML>\n"
          + "<HEAD><TITLE>Opinion Statistics</TITLE>"
          + "<style type=\"text/css\">"
          + "body{margin-top:5%;margin-left:10%;margin-right:10%;background-color:#696969; background-repeat:no-repeat;}"
          + ".container{text-align:center;}"
          + ".left_algn{margin-left:10%; margin-right:10%; text-align:left;}"
          + ".center_div { border:1px gray; margin-left:10%; margin-right:10%;background-color:white; text-align:center; padding:8px; }</style>"
          + "</HEAD>\n"
          + "<div class=\"container\">"
          + " <div class=\"center_div\">"
          + "<script type=\"text/JavaScript\">"
          +

          "function toggle(d)"
          + "{"
          + "	var o=document.getElementById(d);"
          + "o.style.display=(o.style.display=='none')?'block':'none';"
          + "}"
          +

          "</script>"
          + "<h2>Opinion Statistics </h2><br>"
          + "<div class = \"left_algn\">"
          + "<a href=\"javascript:;\" onClick=\"toggle('one');\"><h3>Positive <"
          + pos.size() + "></h3></a>" +

          "<div id=\"one\" style=\"display:none; width:100%;\">"
          + posComment.toString() + "</div>" +

          "<a href=\"javascript:;\" onClick=\"toggle('two');\"><h3>Negative <"
          + neg.size() + "></h3></a>" +

          "<div id=\"two\" style=\"display:none; width:100%;\">"
          + negComment.toString() + "</div>" +

          "<a href=\"javascript:;\" onClick=\"toggle('three');\"><h3>Neutral <"
          + ntr.size() + "></h3></a>" +

          "<div id=\"three\" style=\"display:none; width:100%;\">"
          + ntrComment.toString() + "</div>"

          + "</div></div></div></BODY></HTML>");
    } else if (id != null && id.equals("commentstat")) {

      ArrayList<String> pos = QueryProcess.getInstance().getPositiveComments();
      ArrayList<String> neg = QueryProcess.getInstance().getNegativeComments();
      ArrayList<String> ntr = QueryProcess.getInstance().getNeutralComments();

      StringBuffer posComment = new StringBuffer();

      for (int i = 0; i < pos.size(); i++) {
        String[] index = pos.get(i).split(";;");
        for (int j = 0; j < index.length; j++) {
          posComment.append(index[j]);
          posComment.append("<br>");
        }
        posComment.append("<br><br>");
      }

      StringBuffer negComment = new StringBuffer();

      for (int i = 0; i < neg.size(); i++) {
        String[] index = neg.get(i).split(";;");
        for (int j = 0; j < index.length; j++) {
          negComment.append(index[j]);
          negComment.append("<br>");
        }
        negComment.append("<br><br>");
      }

      StringBuffer ntrComment = new StringBuffer();

      for (int i = 0; i < ntr.size(); i++) {
        String[] index = ntr.get(i).split(";;");
        for (int j = 0; j < index.length; j++) {
          ntrComment.append(index[j]);
          ntrComment.append("<br>");
        }
        ntrComment.append("<br><br>");
      }

      response.setContentType("text/html");
      PrintWriter out = response.getWriter();
      String docType = "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.0 "
          + "Transitional//EN\">\n";
      out.println(docType
          + "<HTML>\n"
          + "<HEAD><TITLE>Comment Statistics</TITLE>"
          + "<style type=\"text/css\">"
          + "body{margin-top:5%;margin-left:10%;margin-right:10%;background-color:#696969; background-repeat:no-repeat;}"
          + ".container{text-align:center;}"
          + ".left_algn{margin-left:10%; margin-right:10%; text-align:left;}"
          + ".center_div { border:1px gray; margin-left:10%; margin-right:10%;background-color:white; text-align:center; padding:8px; }</style>"
          + "</HEAD>\n"
          + "<div class=\"container\">"
          + " <div class=\"center_div\">"
          + "<script type=\"text/JavaScript\">"
          +

          "function toggle(d)"
          + "{"
          + "	var o=document.getElementById(d);"
          + "o.style.display=(o.style.display=='none')?'block':'none';"
          + "}"
          +

          "</script>"
          + "<h2>Comment Statistics </h2><br>"
          + "<div class = \"left_algn\">"
          + "<a href=\"javascript:;\" onClick=\"toggle('one');\"><h3>Positive <"
          + pos.size() + "></h3></a>" +

          "<div id=\"one\" style=\"display:none; width:100%;\">"
          + posComment.toString() + "</div>" +

          "<a href=\"javascript:;\" onClick=\"toggle('two');\"><h3>Negative <"
          + neg.size() + "></h3></a>" +

          "<div id=\"two\" style=\"display:none; width:100%;\">"
          + negComment.toString() + "</div>" +

          "<a href=\"javascript:;\" onClick=\"toggle('three');\"><h3>Neutral <"
          + ntr.size() + "></h3></a>" +

          "<div id=\"three\" style=\"display:none; width:100%;\">"
          + ntrComment.toString() + "</div>"

          + "</div></div></div></BODY></HTML>");

    } else if (id != null && id.equals("moderatorstat")) {

      ArrayList<String> pos = QueryProcess.getInstance()
          .getModeratorPositiveComments();

      ArrayList<String> neg = QueryProcess.getInstance()
          .getModeratorNegativeComments();

      ArrayList<String> ntr = QueryProcess.getInstance()
          .getModeratorNeutralComments();

      int total = QueryProcess.getInstance().getModeratorTotalComment();

      StringBuffer posComment = new StringBuffer();

      for (int i = 0; i < pos.size(); i++) {
        String[] index = pos.get(i).split(";;;");

        for (int j = 0; j < index.length; j++) {

          String[] ind = index[j].split(";;");

          for (int k = 0; k < ind.length; k++) {

            posComment.append(ind[k]);
            posComment.append("<br>");
          }
          posComment.append("<br>");
        }
        posComment.append("<br><br>");
      }

      StringBuffer negComment = new StringBuffer();

      for (int i = 0; i < neg.size(); i++) {
        String[] index = neg.get(i).split(";;;");

        for (int j = 0; j < index.length; j++) {

          String[] ind = index[j].split(";;");

          for (int k = 0; k < ind.length; k++) {
            negComment.append(ind[k]);
            negComment.append("<br>");
          }
          negComment.append("<br>");
        }
        negComment.append("<br><br>");
      }

      StringBuffer ntrComment = new StringBuffer();

      for (int i = 0; i < ntr.size(); i++) {
        String[] index = ntr.get(i).split(";;;");

        for (int j = 0; j < index.length; j++) {

          String[] ind = index[j].split(";;");

          for (int k = 0; k < ind.length; k++) {
            ntrComment.append(ind[k]);
            ntrComment.append("<br>");
          }
          ntrComment.append("<br>");
        }
        ntrComment.append("<br><br>");
      }

      response.setContentType("text/html");
      PrintWriter out = response.getWriter();
      String docType = "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.0 "
          + "Transitional//EN\">\n";
      out.println(docType
          + "<HTML>\n"
          + "<HEAD><TITLE>Moderator Statistics</TITLE>"
          + "<style type=\"text/css\">"
          + "body{margin-top:5%;margin-left:10%;margin-right:10%;background-color:#696969; background-repeat:no-repeat;}"
          + ".container{text-align:center;}"
          + ".left_algn{margin-left:10%; margin-right:10%; text-align:left;}"
          + ".center_div { border:1px gray; margin-left:10%; margin-right:10%;background-color:white; text-align:center; padding:8px; }</style>"
          + "</HEAD>\n"
          + "<div class=\"container\">"
          + " <div class=\"center_div\">"
          + "<script type=\"text/JavaScript\">"
          +

          "function toggle(d)"
          + "{"
          + "	var o=document.getElementById(d);"
          + "o.style.display=(o.style.display=='none')?'block':'none';"
          + "}"
          +

          "</script>"
          + "<h2>Moderator Comment Statistics <"
          + total
          + "> </h2><br>"
          +

          "<a href=\"javascript:;\" onClick=\"toggle('one');\"><h3>Positive <"
          + pos.size()
          + "></h3></a>"
          +

          "<div id=\"one\" style=\"display:none; width:100%;\"><div class=\"left_algn\" >"
          + posComment.toString()
          + "</div></div>"
          +

          "<a href=\"javascript:;\" onClick=\"toggle('two');\"><h3>Negative <"
          + neg.size()
          + "></h3></a>"
          +

          "<div id=\"two\" style=\"display:none; width:100%;\"><div class=\"left_algn\">"
          + negComment.toString()
          + "</div></div>"
          +

          "<a href=\"javascript:;\" onClick=\"toggle('three');\"><h3>Neutral <"
          + ntr.size()
          + "></h3></a>"
          +

          "<div id=\"three\" style=\"display:none; width:100%;\"><div class=\"left_algn\">"
          + ntrComment.toString() + "</div></div>" + "</div></div>"
          + "</BODY></HTML>");
    } else if (id != null && id.equals("issuestat")) {

      ArrayList<String> issues = QueryProcess.getInstance()
          .getIssueWiseCumulativePolarity();
      int total = issues.size()-1;
      
      
      response.setContentType("text/html");
      PrintWriter out = response.getWriter();
      StringBuffer docType = new StringBuffer(
          "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.0 "
              + "Transitional//EN\">\n");
      docType
          .append("<HTML>\n"
              + "<HEAD><TITLE>Issue Statistics</TITLE>"
              + "<style type=\"text/css\">"
              + "body{margin-top:5%;margin-left:20%;margin-right:20%;background-color:#696969; background-repeat:no-repeat;}"
              + ".container{text-align:center;}"
              + ".left_algn{margin-left:10%; margin-right:10%; text-align:left;}"
              + ".center_div { border:1px gray; margin-left:10%; margin-right:10%;background-color:white; text-align:center; padding:8px; }</style>"
              + "</HEAD>\n" + "<div class=\"container\">"
              + " <div class=\"center_div\">"
              + "<script type=\"text/JavaScript\">" +

              "function toggle(d)" + "{" + "	var o=document.getElementById(d);"
              + "o.style.display=(o.style.display=='none')?'block':'none';"
              + "}" +

              "</script>" +

              "<h2>Issue wise Statistics <" + total + "> </h2>");

      docType.append("<div class = \"left_algn\">");
      docType.append(issues.get(total));
      for (int i = 0; i < total; i++) {
        String[] issue = issues.get(i).split(";;");
        String issueText = issue[0];
        ArrayList<String> topicIn = QueryProcess.getInstance().getIssueWiseTopicList(issueText);
        
        docType.append("<a href=\"javascript:;\" onClick=\"toggle('" + i
            + "');\"><h3>" + "Issue: " +issue[0] + "</h3></a>");

        docType.append("<div id=\"" + i
            + "\" style=\"display:none; width:1000px;\">");
        docType.append(issue[1] + "<br>" + issue[2] + "<br>" + issue[3]
            + "<br>" + issue[4] + "<br>" + issue[5] + "<br>");
        
        if(topicIn.size() > 0){
        	
        	docType.append("<h4><font color = \"Pastel Green\">Topics:</font></h4>");
        for(int k = 0; k < topicIn.size(); k++){
        	docType.append(topicIn.get(k)+"<br>");
        }
        
        }
        docType.append("</div>");

      }

      docType.append("</div></div></div></BODY></HTML>");
      out.println(docType.toString());

    } else if (id != null && id.equals("authorstat")) {

      ArrayList<String> authors = QueryProcess.getInstance()
          .getAuthorWiseCumulativePolarity();
      int total = authors.size()-1;

      response.setContentType("text/html");
      PrintWriter out = response.getWriter();
      StringBuffer docType = new StringBuffer(
          "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.0 "
              + "Transitional//EN\">\n");
      docType
          .append("<HTML>\n"
              + "<HEAD><TITLE>Author Statistics</TITLE>"
              + "<style type=\"text/css\">"
              + "body{margin-top:5%;margin-left:20%;margin-right:20%;background-color:#696969; background-repeat:no-repeat;}"
              + ".container{text-align:center;}"
              + ".left_algn{margin-left:10%; margin-right:10%; text-align:left;}"
              + ".center_div { border:1px gray; margin-left:10%; margin-right:10%;background-color:white; text-align:center; padding:8px; }</style>"
              + "</HEAD>\n" + "<div class=\"container\">"
              + " <div class=\"center_div\">"
              + "<script type=\"text/JavaScript\">" +

              "function toggle(d)" + "{" + "	var o=document.getElementById(d);"
              + "o.style.display=(o.style.display=='none')?'block':'none';"
              + "}" +

              "</script>" +

              "<div class=\"container\">" + " <div class=\"center_div\">"
              + "<h2>Author wise Statistics <" + total + "> </h2>");

      docType.append("<div class = \"left_algn\">");
      docType.append(authors.get(total));
      for (int i = 0; i < total; i++) {
        String[] author = authors.get(i).split(";;");

        docType.append("<a href=\"javascript:;\" onClick=\"toggle('" + i
            + "');\"><h3>" + author[0] + "</h3></a>");

        docType.append("<div id=\"" + i
            + "\" style=\"display:none; width:1000px;\">");
        docType.append(author[1] + "<br>" + author[2] + "<br>" + author[3]
            + "<br>" + author[4] + "<br>" + "</div>");

      }
      docType.append("</div></div></div></BODY></HTML>");

      out.println(docType.toString());

    } else if (id != null && id.equals("opinionhold")) {

      ArrayList<String> sents = QueryProcess.getInstance().opinionHolderStats();
      StringBuffer sent = new StringBuffer(sents.get(sents.size() - 1));

      sent.append("<br>");
      sent.append("<br>");
       sent.append("<h3>Opinion Holder:</h3><br>");
      for (int i = 0; i < sents.size() - 1; i++) {
        String s = sents.get(i);
        sent.append(s);
        sent.append("<br><br>");
      }

      response.setContentType("text/html");
      PrintWriter out = response.getWriter();
      String docType = "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.0 "
          + "Transitional//EN\">\n";
      out.println(docType
          + "<HTML>\n"
          + "<HEAD><TITLE>Opinion Holder Statistics</TITLE>"
          + "<style type=\"text/css\">"
          + "body{margin-top:5%;margin-left:20%;margin-right:20%;background-color:#696969; background-repeat:no-repeat;}"
          + ".container{text-align:center;}"
          + ".left_algn{margin-left:10%; margin-right:10%; text-align:left;}"
          + ".center_div { border:1px gray; margin-left:10%; margin-right:10%;background-color:white; text-align:center; padding:8px; }</style>"
          + "</HEAD>\n"
          + "<div class=\"container\">"
          + " <div class=\"center_div\">"
          + "<h2>Opinion Holder Statistics <br><br></h2><div class=\"left_algn\">"
          + sent.toString() + "</div></div></div>" + "</BODY></HTML>");
    }
  }
}
